const webpack = require('webpack')
const babelOptions = require('./babel.config')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const StyleLintPlugin = require('stylelint-webpack-plugin')
module.exports = {
  entry: './src/main.js',
  output: {
    filename: '[name].js'
  },
  optimization: { // 使用treeShaking
    sideEffects: true,
    usedExports: true,
    minimize: true
  },
  module: {
    rules: [
      {
        test: /\.vue$/, // 处理vue文件
        use: 'vue-loader'
      },
      {
        test: /\.js$/, // 处理js文件
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: babelOptions
        }
      },
      {
        test: /\.js$/, // 添加eslint
        exclude: /node_modules/,
        use: 'eslint-loader',
        enforce: 'pre'
      },
      {
        test: /\.less$/, // 处理less文件
        use: [
          'style-loader',
          'css-loader',
          'less-loader'
        ]
      },
      {
        test: /\.css$/, // 处理css文件
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.(jpg|png|gif|bmp|jpeg)$/, // 处理图片资源文件
        use: {
          loader: 'url-loader',
          options: {
            limit: 2 * 1024, // 2KB
            esModule: false
          }
        }
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new VueLoaderPlugin(),
    new webpack.DefinePlugin({
      BASE_URL: '"./"'
    }),
    new HtmlWebpackPlugin({
      title: 'vue-app-base',
      template: './public/index.html'
    }),
    new StyleLintPlugin({
      files: ['src/**/*.vue', 'src/**/*.css']
    })
  ]
}
